<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/14
 * Time: 17:39
 */
namespace App\Http\Controllers\Api\V1_9_3;

use App\Services\AlipayValidateCardInfo\BankCardInfoService;
use App\Services\JMessage\JMessageService;
use App\Services\MessageService;
use App\Services\JMessage\PushMessageService;
use Illuminate\Http\Request;

class MessageController extends BaseController
{
    protected $messageService;

    public function __construct(MessageService $messageService)
    {
        parent::__construct();
        $this->messageService = $messageService;
    }

    public function getProfitList()
    {
        return $this->messageService->getProfitList();
    }

    /**
     * 获取消息列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMessageList(Request $request)
    {
        if (empty($request->msg_type)) {
            return $this->responseJson(422);
        }
        if ('0001' == $request->msg_type) {
            $toUserId = $this->user['invited_code'];
        } else {
            $toUserId = '';
        }
        return $this->messageService->getMessageList($toUserId, $request->msg_type, $request->page);
    }

    /**
     * 获取消息条数
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMessageCount()
    {
        return $this->messageService->getMessageCount($this->user['invited_code']);
    }

    /**
     * 批量标记已读
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchFlagRead(Request $request)
    {
        $ids = json_decode($request->ids);
        if ($request->type == 0) {
            if (empty($ids)) {
                return $this->responseJson(422);
            }
        }
        return $this->messageService->batchFlagRead($ids, $request->type, $this->user['invited_code']);
    }

    /**
     * 调用示例
     * GET Push::getPush($val, $cate)
     * cate：1（根据batchId） 2（根据workno）3查询推送统计（根据batchId）
     * 4查询推送统计（根据workno）5查询别名 6查询标签 7地理位置信息
     *
     * POST Push::postPush($params, $cate)
     * cate：1发送推送 2设置别名 3设置标签
     */
    public function test2($cate = 'get-1')
    {
        # 实例化
        $push = new PushMessageService();
        switch ($cate) {
            case 'get-1':
                # 查询推送（根据batchId）
                $res = $push->getPush('5a7d35723dde6fd72383a96d ', 1);
                break;
            case 'get-2':
                # 查询推送（根据workno）
                $res = $push->getPush(10011, 2);
                break;
            case 'get-3':
                # 查询推送统计（根据batchId）
                $res = $push->getPush('5a7c5a293dde6fd72383a962', 3);
                break;
            case 'get-4':
                # 查询推送统计（根据workno）
                $res = $push->getPush(10011, 4);
                break;
            case 'get-5':
                # 查询别名（根据registrationId）
                $res = $push->getPush('59e6cf592fb6e6ffa300a8b2', 5);
                break;
            case 'get-6':
                # 查询标签（根据registrationId）
                $res = $push->getPush('59e6cf592fb6e6ffa300a8b2', 6);
                break;
            case 'get-7':
                # 地理位置信息接口（根据parentId）
                $res = $push->getPush(3, 7);
                break;
            case 'post-1':
                # 发送推送
                $params = array(
                    'plats' => array(1, 2),
                    # 设置推送范围
                    'target' => 3, 'content' => '推送咯', 'type' => 1, 'tags' => array('tag1', 'tag2'), 'alias' => array(), 'registrationIds' => array(),
                    # 设置Android定制信息
                    'androidTitle' => '通知栏标题栏', 'androidstyle' => 0, 'androidVoice' => 1, 'androidShake' => 1, 'androidLight' => 1,
                    # 设置iOS定制信息
                    'iosProduction' => 1, 'title' => 'iOS Title', 'subtitle' => 'SubTitle', 'sound' => 'default', 'badge' => 1, 'slientPush' => 1, 'contentAvailable' => 1, 'mutableContent' => 1,
                    # 设置推送扩展信息
                    'unlineTime' => 1, 'extras' => array('key1' => 'val-1', 'key2' => 'val-2'),
                );
                $res = $push->postPush($params, 1);
                break;
            case 'post-2':# 设置别名
                $params = array(
                    'registrationId' => '59e6cf592fb6e6ffa300a8b2',
                    'alias' => '150210xxxxx',
                );
                $res = $push->postPush($params, 2);
                break;
            case 'post-3':# 设置标签
                $params = array(
                    'registrationId' => '59e6cf592fb6e6ffa300a8b2',
                    'opType' => 1,
                    'tags' => array('tag1', 'tag2')
                );
                $res = $push->postPush($params, 3);
                break;

            default:

                break;
        }
        print_r($res);
    }

    public function test3()
    {
        $bankCardInfoService = new BankCardInfoService();
        $accNo = '6217282122900679080';
        $cardInfo = $bankCardInfoService->getBankInfo($accNo);
        dd($cardInfo);
        $push = new PushMessageService();
//        $res = $push->getPush('5bbebee47ddfffdd49e2c83a', 1);
        $res = $push->getPush(config('im.mob_push.appKey') . '/0', 8);
        dd($res);
        dd($this->test2('get-1'));
    }


}